import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

import '../../helper/uidata.dart';
import '../../model/jxgzp.dart';
import 'jxgzp_authorty.dart';
import '../../widgets/someCards.dart';

class JxgzpListPage extends StatefulWidget {
  final bool ifAppbar;
  @override
  JxgzpListPage(this.ifAppbar);
  @override
  JxgzpListPageState createState() => new JxgzpListPageState();
}

class JxgzpListPageState extends State<JxgzpListPage> {
  List<JXGZP> RList = <JXGZP>[];
  bool isEnd = false;
  ScrollController _scrollController = new ScrollController();
  bool firstLoading = true;
  bool ifCanAdd = true;
  int canShow = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    if (UIData.CurrentUser!.Authority.indexOf("工程部负责人") != -1) {
      canShow = 0;
    } else {
      canShow = 1;
    }

    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        getMore();
      }
    });

    getNewer();
  }

  Future<Null> delete(JXGZP r1) async {
    // await addOne();

    await JXGZP.Delete(r1);
    setState(() {
      RList.remove(r1);
    });
  }

  onDeleted(JXGZP r1) => {
        setState(() {
          RList.remove(r1);
        })
      };

  Future<Null> getMore() async {
    // await addOne();
    int count = 5;
    if (isEnd == true) return;
    int page = RList.length ~/ count;
    var list1 = await JXGZP.GetList2(canShow, page + 1, count);
    var list2 =
        list1.where((p) => RList.any((x) => x.ID == p.ID) == false).toList();

    setState(() {
      if (list1.length < count) isEnd = true;
      RList.insertAll(RList.length, list2);
    });
  }

  Future<Null> getRefrush() async {
    var list1 = await JXGZP.GetList2(canShow, 1, 20);

    setState(() {
      RList = list1;
    });
  }

  Future<Null> getNewer() async {
    var list1 = await JXGZP.GetList2(canShow, 1, 20);
    setState(() {
      firstLoading = false;
    });

    if (list1.length == 0) return;

    var list2 =
        list1.where((p) => RList.any((x) => x.ID == p.ID) == false).toList();
    if (list2.length == 0) return;

    list2.insertAll(list2.length, RList);

    setState(() {
      firstLoading = false;
      RList = list2;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (RList == null) return new Container();
    // TODO: implement build
    return new Scaffold(
        appBar: widget.ifAppbar ? AppBar(title: Text('泄洪设施检修工作票')) : null,
        floatingActionButton: FloatingActionButton(
            child: Icon(Icons.refresh), onPressed: getRefrush),
        body: firstLoading == true
            ? Center(
                child: SpinKitCircle(
                color: Colors.blue,
              ))
            : Container(
                margin: EdgeInsets.only(top: 8.0),
                child: new RefreshIndicator(
                  child: ListView.builder(
                    itemCount: RList.length + 1,
                    controller: _scrollController,
                    physics: const AlwaysScrollableScrollPhysics(),
                    itemBuilder: (BuildContext context, int index) {
                      if (index != RList.length) {
                        return SomeCard.JXGZPCard(
                            context, RList[index], getRefrush);
                      } else {
                        if (isEnd == true) {
                          return Container(
                              margin: EdgeInsets.all(12.0),
                              child: Center(
                                  child: Text(
                                "没有更多了",
                                style: TextStyle(color: Colors.grey),
                              )));
                        } else {
                          return new Container();
                        }
                      }
                    },
                  ),
                  onRefresh: getNewer,
                )));
  }
}
